package com.journey.org.app

import com.journey.org.entity.*
import com.mvvmhabit.org.http.RetrofitManager
import com.mvvmhabit.org.http.TianXingResponse
import com.mvvmhabit.org.utils.DeferredUtil

class HttpManager {

    companion object {
        private var INSTANCE: HttpManager? = null

        @Synchronized
        fun getInstance(): HttpManager {
            if (INSTANCE == null) {
                return HttpManager()
            }
            return INSTANCE as HttpManager
        }
    }

    /********************************** 干货接口API ***********************************/


    suspend fun getBannerListData(): GankIoResponse<List<BannerEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadBannerListDataAsync()!!
        )
        /*     ?.compose(RxUtil.schedulersTransformer())
             ?.compose(RxUtil.exceptionTransformer())*/
    }

    suspend fun getWeekHottestList(
        hotType: String,
        category: String,
        count: Int
    ): GankIoResponse<List<WeekHottestEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadWeekHottestListAsync(hotType, category, count)!!
        )
    }

    suspend fun getGankSearchContent(
        searchContent: String,
        type: String,
        page: Int
    ): GankIoResponse<List<SearchGankEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadGankSearchContentAsync(searchContent, type, page)!!
        )
    }

    // 干货美女
    suspend fun loadGankGirlList(page: Int): GankIoResponse<List<GankGirlEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadGankGirlListAsync(page)!!
        )
    }

    // 文章子分类
    suspend fun loadGankIoMenuList(): GankIoResponse<List<GankMenuEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadGankIoMenuListAsync()!!
        )
    }

    // 文章子分类
    suspend fun loadGankIoArticleList(
        type: String,
        page: Int
    ): GankIoResponse<List<GankArticleEntity>>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadGankIoArticleListAsync(type, page)!!
        )
    }

    // 文章子分类
    suspend fun loadGankArticleDetail(id: String): GankIoResponse<GankArticleDetailEntity>? {
        return DeferredUtil.requests(
            RetrofitManager.getInstance("https://gank.io/")?.create(API::class.java)
                ?.loadGankArticleDetailAsync(id)!!
        )
    }

    /*********************************************** 天气接口 **********************************/
    suspend fun loadWeatherInfo(city: String): WeatherEntity? {
        return RetrofitTestManager.getInstance("http://wthrcdn.etouch.cn/")
            ?.create(API::class.java)
            ?.loadWeatherInfoAsync(city)?.await()
    }

    /********************************************         *************************************/
    // 获取首页推荐列表数据
    suspend fun loadHomeRecommendListData(page: Int): BaoBaBaseResponse<List<HomeRecommendEntity>>? {
        return DeferredUtil.requests(
            RetrofitOtherManager.getInstance("http://baobab.kaiyanapp.com/")
                ?.create(API::class.java)
                ?.loadHomeRecommendListDataAsync(page)!!
        )
    }

    // 获取首页推荐列表数据
    suspend fun loadVideoDetailRecommendList(videoId: String): BaoBaBaseResponse<List<HomeRecommendEntity>>? {
        return DeferredUtil.requests(
            RetrofitOtherManager.getInstance("http://baobab.kaiyanapp.com/")
                ?.create(API::class.java)
                ?.loadVideoDetailRecommendListAsync(videoId)!!
        )
    }

    // 获取首页小视频列表数据 - 网易视频
    suspend fun loadNeteaseVideoList(id: String, page: Int): NeteaseVideoEntity? {
        return RetrofitNeteaseManager.getInstance()
            ?.create(API::class.java)
            ?.loadNeteaseVideoListAsync(id, page)?.await()
    }

    // 获取网易新闻列表数据
    suspend fun loadNeteaseNewsList(
        type: String,
        id: String,
        page: Int
    ): HashMap<String, List<NeteaseNewsEntity>>? {
        return RetrofitNeteaseManager.getInstance()
            ?.create(API::class.java)
            ?.loadNeteaseNewsListAsync(type, id, page)?.await()
    }

    // 获取网易新闻详情数据
    suspend fun loadNeteaseNewsDetail(id: String): HashMap<String, NeteaseNewsDetailEntity>? {
        return RetrofitNeteaseManager.getInstance()
            ?.create(API::class.java)
            ?.loadNeteaseNewsDetailAsync(id)?.await()
    }

    // 天行-美女数据
    suspend fun loadBeautyGirlList(
        page: Int, word: String
    ): TianXingResponse<List<BeautyGirlEntity>>? {
        return DeferredUtil.requests(
            if (word.isNotEmpty()) {
                RetrofitTianXingManager.getInstance()
                    ?.create(API::class.java)
                    ?.loadBeautyGirlWordListAsync(
                        "4100de888f89fc1b5a85db5526f51aa8", 20, page, word
                    )!!
            } else {
                RetrofitTianXingManager.getInstance()
                    ?.create(API::class.java)
                    ?.loadBeautyGirlListAsync("4100de888f89fc1b5a85db5526f51aa8", 20, page)!!
            }
        )
    }

    // 玩Android - banner
    suspend fun loadPlayAndroidBannerList(): PlayAndroidResponse<List<PlayAndroidBannerEntity>>? {
        return DeferredUtil.requests(
            RetrofitPlayAndroidManager.getInstance()
                ?.create(API::class.java)
                ?.loadPlayAndroidBannerListAsync()!!
        )
    }

    // 玩Android - home
    suspend fun loadPlayAndroidHomeList(page: Int): PlayAndroidResponse<PlayAndroidHomeEntity>? {
        return DeferredUtil.requests(
            RetrofitPlayAndroidManager.getInstance()
                ?.create(API::class.java)
                ?.loadPlayAndroidHomeListAsync(page)!!
        )
    }

    // 玩Android - 项目目录列表
    suspend fun loadPlayAndroidMenuList(): PlayAndroidResponse<List<PlayAndroidMenuEntity>>? {
        return DeferredUtil.requests(
            RetrofitPlayAndroidManager.getInstance()
                ?.create(API::class.java)
                ?.loadPlayAndroidMenuListAsync()!!
        )
    }

    // 玩Android - 项目列表
    suspend fun loadPlayAndroidListById(
        page: Int,
        cid: String
    ): PlayAndroidResponse<PlayAndroidProjectEntity>? {
        return DeferredUtil.requests(
            RetrofitPlayAndroidManager.getInstance()
                ?.create(API::class.java)
                ?.loadPlayAndroidListByIdAsync(page, cid)!!
        )
    }
}
